import React from "react";

import PageContent from '../../PageContent'

import { ajax ,showResultMessage} from '../../common';

import {UIDataTableView} from '../../uicomponents/UIDataComponentView';

import UIFormView from '../../uicomponents/UIFormView';
/** 
 * 产品数据库管理应用
*/
export default class ProductDatabaseManageApp extends React.Component {

	render() {
		return (
			<PageContent {...this.props} pageInstance={this} >
				<ProductDatabaseListPanel />
			</PageContent>
		);
	}
}

/** 
 * 产品数据库列表
*/
class ProductDatabaseListPanel extends React.Component {

	constructor(props) {
		super(props);
		this.state = {
			formTitle: '',
		}
	}


	actionHandler = (actionEvent) => {

		const { databaseListRef, databaseFormRef, selectProductDatabaseIds } = this;

		return {
			refresh: () => {
				databaseListRef.loadData();
			},
			addNewDatabase: () => {
				this.setState({ formTitle: '新增产品数据库' });
				setTimeout(() => {
					databaseFormRef.openModal();
				});
			},
			doEdit : () => {
				this.setState({ formTitle: '编辑产品数据库' });
				setTimeout(() => {
					databaseFormRef.openModal(() => {
						databaseFormRef.setFormData(actionEvent.params);
					});
				});
			},
			deleteProductDatabase: () => {

				let selectedIds = null;

				if (actionEvent.params != null) {
					selectedIds = [actionEvent.params.id];
				}
				else {
					selectedIds = selectProductDatabaseIds;
				}

				ajax('PRODUCT/MODEL/productDatabase/deleteProductDatabase', { ids: selectedIds }, (data) => {
					databaseListRef.loadData();
				});
			},
			doSave: () => {
				databaseFormRef.getFormData((formData) => {
					ajax('PRODUCT/MODEL/productDatabase/saveProductdatabase',formData,(data) => {
						databaseFormRef.closeModal();
						databaseListRef.loadData();
						showResultMessage({
							success : true,
                            message : 'message.saveSuccess',
						});
					});
				});
			}
		};
	}

	render() {

		const databaseListTableProps = {
			actionHandler: this.actionHandler,
			actionGroupId: 'productDatabaseActionGroup',
			rowActionGroupId: 'productDatabaseRowActionGroup',
			tableConfigId: 'productDatabaseTable',
			dataUrl: 'PRODUCT/MODEL/productDatabase/queryProductDatabases',
			selectMode: 'multi',
			autoLoad: true,
			ref: (inst) => { this.databaseListRef = inst },
			onSelectChange:
				(selectRowKeys) => {
					this.selectProductDatabaseIds = selectRowKeys;
				}
			,
			setToolbarState: (toolBar, selectRowKeys) => {
				if (selectRowKeys.length > 0) {
					toolBar.enable('delete');
				}
				else {
					toolBar.disable('delete');
				}
			}
		};

		const databaseFormProps = {
			ref: (inst) => { this.databaseFormRef = inst },
			formConfigId: 'productDatabaseForm',
			actionGroupId: 'saveActionGroup',
			actionHandler: this.actionHandler,
			inPop: true,
			destroyOnClose: true,
			title: this.state.formTitle,
		}

		return (
			<React.Fragment>
				<UIDataTableView {...databaseListTableProps} />
				<UIFormView {...databaseFormProps} />
			</React.Fragment>
		);
	}
}